﻿#pragma once

#include <QTranslator>
#include <QScopedPointer>
#include <QApplication>
#include <QLocale>

class InsourcedTranslator : public QTranslator {
    Q_OBJECT
    inline static QMap<QString, QMap<QString, QString>> translateTable;
public:
    struct TranslateResult {
        bool found{ false };
        QString text;
    };
    ~InsourcedTranslator();
    InsourcedTranslator(QObject *parent = nullptr);
    static void LoadTranslations();
    static void SetLanguage(const QLocale::Language &language, const QLocale::Country &country);
    static void SetLanguage(const QLocale &locale);
    static QList<QLocale> AvailableLanguages();

public:
    bool isEmpty() const override { return false; }
    QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const override;

private:
    TranslateResult SearchTranslation(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
    void AddUntranslatedItem(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
    static void SaveUntranslatedItemToFile();
};

// Debug
#include <QDebug>
#include <QLoggingCategory>
inline static QLoggingCategory insourced_translator("insourced_translator");
#define HI_DEBUG qDebug(insourced_translator).noquote() << __FUNCTION__
